package com.woniuxy.bookmall.service;

import com.github.pagehelper.PageInfo;
import com.woniuxy.bookmall.repository.EsRepository;
import com.woniuxy.bookmall.vo.EsBook;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class EsService {

    @Autowired
    RestHighLevelClient client ;

    @Autowired
    EsRepository repository ;
    public List<EsBook> findEsBookByType(int type , int count){
        String field = "buycount" ;
        if (type == 0) {
            field = "buycount";
        }else if (type == 1) {
            field = "publishdate" ;
        }else if (type == 2){
            field = "collectioncount";
        }
        //设置排序(排序方式，正序还是倒序，排序的 id)
        Sort sort = Sort.by(Sort.Direction.DESC, field);
        //设置分页
        PageRequest pageRequest = PageRequest.of(0, count, sort);
        //分页查询
        Page<EsBook> page = repository.findAll(pageRequest);
        List<EsBook> list = page.toList();
        return list ;
    }


    public PageInfo<EsBook> findEsBookByBookType(int id, String bname, int now, int size) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("bookmall");
        //设置一个查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //设置分页
        searchSourceBuilder.from(now);
        searchSourceBuilder.size(size);

        //设置查询方式
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (bname != null && !"".equals(bname.trim())){
            //1. 设置关键词查询 因为关键词查询里 包含一个bool查询
            BoolQueryBuilder boolChild = QueryBuilders.boolQuery();
            //子bool中 是 should 查询  or
            MatchQueryBuilder match1 = QueryBuilders.matchQuery("name",bname);
            boolChild.should(match1);
            MatchQueryBuilder match2 = QueryBuilders.matchQuery("detail",bname);
            boolChild.should(match2);

            boolQueryBuilder.must(boolChild);
        }
        if (id != 0) {
            //2.设置类型查询
            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("typeid",id);
            boolQueryBuilder.must(termQueryBuilder);
        }

        searchSourceBuilder.query(boolQueryBuilder);//把查询条件设置到 searchSourceBuilder

        searchRequest.source(searchSourceBuilder);

        //执行查询
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        //解析查询结果
        SearchHits hits = search.getHits(); //查询结果
        TotalHits total = hits.getTotalHits();  //总数据量
        System.out.println("查询到的总数量:"+total.value);
        for (SearchHit s :hits) {
            System.out.println(s.getSourceAsString());
        }
        //封装一个 分页对象

        return null;
    }

}
